package demo1.NiceDemo.mouse;

import af.swing.layout.HLayout;
import af.swing.layout.VLayout;
import demo1.Layouts.LayoutBox;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Mice extends JFrame {
    LayoutBox root = new LayoutBox();
    MyMouse mouse = new MyMouse();
    JButton buttonZ = new JButton("撤销");
    JButton buttonY = new JButton("重做");
    JButton buttonC = new JButton("结束");
    JButton buttonS = new JButton("保存");
    List<MyMouse.Lines> mem = new ArrayList<>();
    JLabel label = new JLabel();

    public Mice(String title) {
        super(title);
        this.setContentPane(root);
        root.layout(new VLayout(4));
        root.setBorder(BorderFactory.createEmptyBorder(6,6,6,6));
        root.add(mouse);
        root.add(buttonLayout());
        Init();
    }
    public LayoutBox buttonLayout() {
        LayoutBox box = new LayoutBox();
        box.layout(new HLayout(4));
        box.preferredHeight(30);
        box.add(label,"1w");
        box.add(buttonZ,"1w");
        box.add(buttonY,"1w");
        box.add(buttonC,"1w");
        box.add(buttonS,"1w");
        return box;
    }
    public void Init() {
        mouse.setPreferredSize(new Dimension(350, 200));
        mouse.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                super.mousePressed(e);
            }
        });

        buttonZ.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                MyMouse.Lines ret = mouse.CtrlZ();
                if (ret != null) {
                    mem.add(ret);
                }
            }
        });

        buttonY.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (mem.size() == 0) {
                    return;
                }else {
                    mouse.CtrlY(mem.get(mem.size() - 1));
                    mem.remove(mem.get(mem.size() - 1));
                }
            }
        });

        buttonC.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                mouse.clearScreen();
                label.setBackground(mouse.getPenColor());
            }
        });

        buttonS.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                FileNameExtensionFilter filter = new FileNameExtensionFilter("图片文件","jpg");
                int ret = chooser.showSaveDialog(null);
                if(ret == JFileChooser.APPROVE_OPTION) {
                    File file = chooser.getSelectedFile();
                    try {
                    Util.snapshot(mouse,file);
                    }catch (RuntimeException err) {
                        JOptionPane.showMessageDialog(null,"此空间拒绝访问");
                    }
                }
            }
        });

        label.setOpaque(true);
        label.setBorder(BorderFactory.createLineBorder(new Color(169, 169, 169),1));
        label.setBackground(mouse.getPenColor());
        label.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                mouse.changeFlag();
                label.setBackground(mouse.getPenColor());
            }
        });
    }
}
